﻿Imports DevExpress.XtraGrid
Imports DevExpress.XtraBars
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports Control_Vuelos_BL
Imports Control_Vuelos_BE
Imports DevExpress.Utils

Public Class ModAdm_Manifiesto
    Private ReadOnly _Pasajero As New PasajeroBL()
    Private ReadOnly _Reserva As New V_ReservaBL()
    Private _PasajeroBE As New M_PasajeroBE
    Private _ReservaBE As New V_ReservaBE

    Private Sub ModAdm_Manifiesto_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PopupMenu1.ItemLinks.Add(New BarButtonItem(BarManager1, "Marcar Jefe de Vuelo"))
        PopupMenu1.ItemLinks.Add(New BarButtonItem(BarManager1, "Remove Marca"))
        PopupMenu1.ItemLinks.Add(New BarButtonItem(BarManager1, "Enviar a otro VUELO"))
        ToolTipController1.ShowBeak = True
        GridControl1.ToolTipController = ToolTipController1
        ToolTipController1.ToolTipType = DevExpress.Utils.ToolTipType.SuperTip
    End Sub

    Private Sub BarButtonItem1_ItemClick_1(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        If GridView1.RowCount > 0 Then
            Dim Rows As New ArrayList()
            Rows.Add(GridView1.GetDataRow(Me.GridView1.GetSelectedRows()(0)))
            Dim Row As DataRow = CType(Rows(0), DataRow)
            If Not Row Is Nothing Then
                If Row("FlgLock") = 1 Then
                    GridView1.SetRowCellValue(GridView1.GetSelectedRows()(0), "FlgLock", 0)
                    BarButtonItem1.Caption = "Confirmar"
                    BarButtonItem1.ImageIndex = 2
                Else
                    GridView1.SetRowCellValue(GridView1.GetSelectedRows()(0), "FlgLock", 1)
                    BarButtonItem1.Caption = "Sin Confirmar"
                    BarButtonItem1.ImageIndex = 3
                End If
            End If
        End If
    End Sub

    Private Sub BarManager1_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarManager1.ItemClick
        If e.Item.Caption = "Marcar Jefe de Vuelo" Then
            If GetBuscar("FlgResponsable", True) Then
                GridView1.SetRowCellValue(GridView1.GetSelectedRows()(0), "FlgResponsable", 1)
            End If
        End If
        If e.Item.Caption = "Remove Marca" Then
            GridView1.SetRowCellValue(GridView1.GetSelectedRows()(0), "FlgResponsable", 0)
        End If
        If e.Item.Caption = "Enviar a otro VUELO" Then
            If GridView1.GetRowCellValue(GridView1.GetSelectedRows()(0), "FlgLock") = 0 Then
                Using sFrmEnvio As New frmAdm_ManifiestoVuelo()
                    sFrmEnvio.CodViaje = Trim(GridView1.GetRowCellValue(GridView1.GetSelectedRows()(0), "CodViaje"))
                    sFrmEnvio.CodReserva = Trim(GridView1.GetRowCellValue(GridView1.GetSelectedRows()(0), "CodReserva"))
                    sFrmEnvio.SLIItem.Text = String.Format("{0} {1} {2}", Trim(GridView1.GetRowCellValue(GridView1.GetSelectedRows()(0), "AppPasajero")), Trim(GridView1.GetRowCellValue(GridView1.GetSelectedRows()(0), "ApmPasajero")), Trim(GridView1.GetRowCellValue(GridView1.GetSelectedRows()(0), "NomPasajero")))
                    sFrmEnvio.ShowDialog()
                End Using
            Else
                MessageBox.Show(" Este pasajero ya está CONFIRMADO y no puede ser removido del vuelo ", "Manifiesto", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If            
        End If
    End Sub

    Private Function GetRowHandleByColumnValue(ByVal view As GridView, ByVal ColumnFieldName As String, ByVal value As Object) As Integer
        Dim result As Integer = GridControl.InvalidRowHandle
        For i As Integer = 0 To view.RowCount - 1
            If view.GetDataRow(i)(ColumnFieldName).Equals(value) Then
                Return i
            End If
        Next i
        Return result
    End Function

    Public Function GetBuscar(ByVal p_campo As String, ByVal p_valor As Object) As Boolean
        Dim rowHandle As Integer = GetRowHandleByColumnValue(GridView1, p_campo.Trim, p_valor)
        If rowHandle <> GridControl.InvalidRowHandle Then
            GridView1.FocusedColumn = GridView1.Columns.ColumnByFieldName(p_campo.Trim)
            GridView1.FocusedRowHandle = rowHandle
            If GridView1.IsRowVisible(rowHandle) = RowVisibleState.Hidden Then
                GridView1.MakeRowVisible(rowHandle, False)
            End If
            GridView1.ShowEditor()
            If p_campo = "FlgResponsable" Then
                MessageBox.Show(" Ya se ha seleccionado un Jefe de Vuelo ", "Manifiesto", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            If p_campo = "NroDocumentoId" Then
                MessageBox.Show(" La Persona ya está registrada en este VUELO", "Manifiesto", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub GridView1_FocusedRowChanged1(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged
        If GridView1.RowCount > 0 Then
            Dim Rows As New ArrayList()
            Rows.Add(GridView1.GetDataRow(Me.GridView1.GetSelectedRows()(0)))
            Dim Row As DataRow = CType(Rows(0), DataRow)
            If Not Row Is Nothing Then
                If Row("EstadoManifiesto").ToString = "1" Then
                    BtnCerradoParcial.Enabled = False
                Else
                    BtnCerradoParcial.Enabled = True
                End If
                If Row("FlgLock") = 1 Then
                    BarButtonItem1.Caption = "Sin Confirmar"
                    BarButtonItem1.ImageIndex = 3
                Else
                    BarButtonItem1.Caption = "Confirmar"
                    BarButtonItem1.ImageIndex = 2
                End If
            End If
        End If
    End Sub

    Private Sub GridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs) Handles GridView1.PopupMenuShowing
        Dim View As GridView = CType(sender, GridView)
        Dim HitInfo As GridHitInfo = View.CalcHitInfo(e.Point)
        If HitInfo.InRowCell Then
            View.FocusedRowHandle = HitInfo.RowHandle
            PopupMenu1.ShowPopup(BarManager1, View.GridControl.PointToScreen(e.Point))
        End If
    End Sub

    Private Sub ToolTipController1_GetActiveObjectInfo(ByVal sender As Object, ByVal e As DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs) Handles ToolTipController1.GetActiveObjectInfo
        If e.SelectedControl IsNot GridControl1 Then
            Return
        End If
        Dim info As ToolTipControlInfo = Nothing
        Dim sTooltip1 As New SuperToolTip()
        Try
            Dim view As GridView = TryCast(GridControl1.GetViewAt(e.ControlMousePosition), GridView)
            If view Is Nothing Then
                Return
            End If
            Dim hi As GridHitInfo = view.CalcHitInfo(e.ControlMousePosition)
            If hi.RowHandle < 0 Then
                Return
            End If
            Dim titleItem1 As New ToolTipTitleItem()
            Const ImageFileName As String = "\SG_Control_Vuelos\Control_Vuelos_GUI\User.png"
            Dim image As Image
            Dim item1 As New ToolTipItem()
            ' load image
            If (Not System.IO.File.Exists(ImageFileName)) Then
                '  MsgBox("Image file not found: " & ImageFileName)
            Else
                image = image.FromFile(ImageFileName)
                item1.Image = image
            End If
            ' Cargar datos de la Reserva y Pasajero 
            _ReservaBE = _Reserva.TraerReserva(view.GetRowCellValue(hi.RowHandle, "CodReserva"))
            _PasajeroBE = _Pasajero.TraerPasajero(view.GetRowCellValue(hi.RowHandle, "CodPasajero"))
            ' End Carga
            item1.Text = String.Format("<b>{0}</b>" & Constants.vbLf & "<b>DNI</b>: {1}" & Constants.vbLf & " <b>EMail</b>:{2}" & Constants.vbLf & "<b>Fecha que reservo</b>: {3}" & Constants.vbLf & "<b>Telefono</b>: {4}" & Constants.vbLf & " <b>RPC</b>:{5}" & Constants.vbLf & " <b>Nacionalidad</b>:{6}" & Constants.vbLf & " <b>Cumple los requisitos</b>:  {7}" & Constants.vbLf & " <b>Motivo del Viaje</b>:{8}" & Constants.vbLf & " <b>Empresa/Terceros</b>:{9}" & Constants.vbLf & " <b>Fecha Viaje :</b> {10}", _PasajeroBE.AppPasajero & " " & _PasajeroBE.ApmPasajero & " , " & _PasajeroBE.NomPasajero, _PasajeroBE.NroDocumentoId, _PasajeroBE.MailPasajero, Format(_ReservaBE.FchReserva, "dd/MM/yyyy"), _PasajeroBE.TlfPasajero, _PasajeroBE.RpcPasajero, _PasajeroBE.NacionalidadPasajero, IIf(_ReservaBE.FlgRequisitos, "Si", "No"), _ReservaBE.MotivoViaje, view.GetRowCellValue(hi.RowHandle, "Desanl"), Format(_ReservaBE.FchViaje, "dd/MM/yyyy"))
            sTooltip1.Items.Add(item1)
            info = New ToolTipControlInfo(hi.HitTest, "")
            info.SuperTip = sTooltip1
        Finally
            e.Info = info
        End Try
    End Sub
End Class

